package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.common.util.DateUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.dao.ISalPostDao;
import com.boss.salary.entity.SalPersonPostRef;
import com.boss.salary.entity.SalPersonType;
import com.boss.salary.entity.SalPost;
import com.boss.salary.service.ISalPostService;
import com.boss.sys.utils.SysUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;

@Service
public class SalPostServiceImpl implements ISalPostService {

    @Autowired
    private ISalPostDao iSalPostDao;

    @Override
    public PageDTO<SalPost> querySalPostPage(PaginationDTO paginationDTO) {
        List<SalPost> salsalPost = iSalPostDao.querySalPostPage(paginationDTO);
        int total = iSalPostDao.querySalPostCount();
        PageDTO<SalPost> PageDTO = new PageDTO<>();
        PageDTO.setRows(salsalPost);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    public int queryDataByCode(String code) {
        return iSalPostDao.queryDataByCode(code);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertSalPost(SalPost salPost) {
        salPost.setId(SysUtils.generateGuid());
        salPost.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        salPost.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        iSalPostDao.insertSalPost(salPost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSalPost(SalPost salPost) {
        salPost.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
        iSalPostDao.updateSalPost(salPost);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteSalPost(String id) {
        iSalPostDao.deleteSalPost(id);
    }

    @Override
    public List<SalPost> querySalPostAll(Map<String, Object> queryParam) {
        return iSalPostDao.querySalPostAll(queryParam);
    }

    @Override
    public PageDTO<Map<String, Object>> querySalPersonPostRefPage(PaginationDTO paginationDTO) {
        List<Map<String, Object>> map = iSalPostDao.querySalPersonPostRefPage(paginationDTO);
        int total = iSalPostDao.querySalPersonPostRefCount();
        PageDTO<Map<String, Object>> PageDTO = new PageDTO<>();
        PageDTO.setRows(map);
        PageDTO.setTotal(total);
        return PageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveSalPersonPostRef(SalPersonType salPersonType) {
        iSalPostDao.deleteSalPersonPostRefByPersonTypeId(salPersonType.getId());
        if (!StringUtils.isEmpty(salPersonType.getSalPostList())) {
            for (String salPostId : salPersonType.getSalPostList()) {
                SalPersonPostRef salPersonPostRef = new SalPersonPostRef();
                salPersonPostRef.setId(SysUtils.generateGuid());
                salPersonPostRef.setPersonTypeId(salPersonType.getId());
                salPersonPostRef.setPostId(salPostId);
                salPersonPostRef.setCreateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                salPersonPostRef.setUpdateTime(String.valueOf(DateUtils.getCurrentTimeStamp()));
                iSalPostDao.insertSalPersonPostRef(salPersonPostRef);
            }
        }
    }

    @Override
    public List<SalPost> querySalPostByPersonTypeId(String personTypeId) {
        return iSalPostDao.querySalPostByPersonTypeId(personTypeId);
    }

}
